package com.reggie.background.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.reggie.background.common.R;
import com.reggie.background.common.ThreadLocalUtils;
import com.reggie.background.entity.AddressBook;
import com.reggie.background.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/addressBook")
public class AddressBookContoller {
    @Autowired
    private AddressBookService addressBookService;
    @GetMapping("/list")
    public R<List<AddressBook>> list() {
        Long id = ThreadLocalUtils.getThreadLocalValue();
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId, id);
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);
    }
    @PostMapping
    public R<String> addressBook(@RequestBody AddressBook addressBook){
        addressBook.setUserId(ThreadLocalUtils.getThreadLocalValue());
        boolean result = addressBookService.save(addressBook);
        if (result) {
            return R.success("成功添加地址！");
        }
        return  R.error("添加地址失败！");
    }
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody Map<String,Long> maps){
        //先让所有地址都取消了默认地址
        LambdaUpdateWrapper<AddressBook> qw=new LambdaUpdateWrapper<>();
        qw.eq(AddressBook::getUserId,ThreadLocalUtils.getThreadLocalValue());
        AddressBook addressBook=new AddressBook();
        addressBook.setIsDefault(0);
        addressBookService.update(addressBook,qw);
        //再将该id的地址设置成默认地址
        AddressBook addressBook1=new AddressBook();
        addressBook1.setIsDefault(1);
        addressBook1.setId(maps.get("id"));
        addressBookService.updateById(addressBook1);
        return R.success("设置默认地址成功");
    }
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> query = new LambdaQueryWrapper<AddressBook>();
        query.eq(AddressBook::getUserId,ThreadLocalUtils.getThreadLocalValue());
        query.eq(AddressBook::getIsDefault,1);
        AddressBook result = addressBookService.getOne(query);
        return R.success(result);
    }
    @GetMapping("/{id}")
    public R<AddressBook> getAddressBook(@PathVariable Long id){
        AddressBook byId = addressBookService.getById(id);
        return R.success(byId);

    }
    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("更新成功！");
    }
    @DeleteMapping
    public R<String> deleteAddressBook( Long ids){
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }

}
